<?php
/**
 * Hotro_Controller
 * - Controller for module page Hotro
 * @author ANHTAM
 * @since 1.1 - Sep 27, 2011
 * $Id: $
 */
// Require module for controller
require_once(LIB_DAO_DIR . "/MHotro.class.php");

class Hotro_Controller extends ControllerAdmin {
	/**
     * - Constructor
     */
    public function __construct() {
    	parent::__construct();
    	$this->flagMenuHoTro = true;
    }
    
    /**
     * actionIndex
     * - page index of controller
     */
    public function actionIndex() {
    	// Process init
        $objView = new ViewAdmin();
        $this->tpl_title = 'Danh sách nhân viên';
        $this->tpl_title_list = 'Support';
        
    	if (!$this->loginFlag || $this->dataUser['quyen'] > '1') {
        	FunctionCommon::sendRedirect();
        }
        
        $ojbhotro = new MHotro();
		
        if ($this->isPost()) {
        	$dataPost = FunctionCommon::getPost(array('itemID', 'mode', 'type', 'checkdel'));
	        
	        // Delete by id
	        if ($dataPost['mode'] == 'delete') {
	        	$ojbhotro->delete(array('id'=>$dataPost['itemID']));
	        }
	        
	        // Delete all by array id
	    	if ($dataPost['mode'] == 'deleteall') {
	    		$arrPost = $dataPost['checkdel'];
	    		if (count ($arrPost) > 0) {
	    			foreach ($arrPost as $aP) {
	        			$ojbhotro->delete(array('id'=>$aP));
	    			}
	    		}
	        }
	        
	        // Up
	        if ($dataPost['mode'] == 'up') {
	        	$id = $dataPost['itemID'];
	        	$seq = $dataPost['type'];
	        	$seqMin = $ojbhotro->getMinMax(array("seq"=>array("math"=>">", "value"=>$seq)));
	        	$idMin = $ojbhotro->getIdBySeq($seqMin);
	        	
	        	$ojbhotro->setAttribute('seq', $seqMin);
	        	$ojbhotro->update(array('id' => $id));
	        	$ojbhotro->emptyAttribute();
	        	$ojbhotro->setAttribute('seq', $seq);
	        	$ojbhotro->update(array('id' => $idMin));
	        }
	        
	    	// Down
	        if ($dataPost['mode'] == 'down') {
	        	$id = $dataPost['itemID'];
	        	$seq = $dataPost['type'];
	        	$seqMax = $ojbhotro->getMinMax(array("seq"=>array("math"=>"<", "value"=>$seq)), true);
	        	$idMax = $ojbhotro->getIdBySeq($seqMax);
	        	
	        	$ojbhotro->setAttribute('seq', $seqMax);
	        	$ojbhotro->update(array('id' => $id));
	        	$ojbhotro->emptyAttribute();
	        	$ojbhotro->setAttribute('seq', $seq);
	        	$ojbhotro->update(array('id' => $idMax));
	        }
        }
        
        $this->maxseq = $ojbhotro->getMinMax(array(), true);
        $this->minseq = $ojbhotro->getMinMax(array());
        
        // List all
    	$this->arrContent = $ojbhotro->getAllByAttribute(array(), array());

        // Process view
        $objView->assignobj($this);
        $objView->display();
    }
    
	/**
     * actionEdit
     * - page edit of controller
     */
    public function actionEdit() {
    	
    	// Process init
        $objView = new ViewAdmin();
        $this->tpl_title = 'Sửa nhân viên';
        $this->tpl_button = "Thêm mới";
        $this->tpl_title_list = 'Support Add/Edit';
        $this->dialog = false;
        
    	if (!$this->loginFlag || $this->dataUser['quyen'] > '1') {
        	FunctionCommon::sendRedirect();
        }
        $this->id = '';
        $this->dataPost = array('itemID' => '', 'mode' => '', 'type' => '', 'name' => '', 'phone' => '', 'nickname' => '');
        $ojbhotro = new MHotro();
        if ($this->isPost()) {
        	$dataPost = FunctionCommon::getPost(array_keys($this->dataPost));
        	$this->id = $dataPost['itemID'];
        	
        	if ($dataPost['mode'] != '') {
        		// Update database
        		if ($dataPost['type'] == 'edit' && $this->id != '') {
        			$ojbhotro->setAttribute('name', $dataPost['name']);
        			$ojbhotro->setAttribute('phone', $dataPost['phone']);
        			$ojbhotro->setAttribute('nickname', $dataPost['nickname']);
	        		$ojbhotro->update(array('id' => $this->id));
	        		$this->dialog = true;
        		}
        		
        		// Insert Database
        		if ($dataPost['type'] == 'edit' && $this->id == '') {
        			$ojbhotro->setAttribute('seq', $ojbhotro->getMinMax(array(), true) + 1);
		        	$ojbhotro->setAttribute('name', $dataPost['name']);
		        	$ojbhotro->setAttribute('phone', $dataPost['phone']);
		        	$ojbhotro->setAttribute('nickname', $dataPost['nickname']);
		        	$ojbhotro->insert();
		        	$this->dialog = true;
        		}
				$this->dataPost = $dataPost;
        	} else {
        		$this->tpl_button = "Sửa";
	        	$this->dataPost = $ojbhotro->getByPrimaryKey($this->id);
        	}
        }
        
        // Process view
        $objView->assignobj($this);
        $objView->display();
    }
}
?>

